<?php
function seitcm_extend_update_student_form($user_id){
	$userID = $user_id->ID;
	global $profileuser;
	$userRole = $profileuser->roles[0];
	$seitcm_user_class = get_user_meta($userID, 'class', true);
	$seitcm_user_school = get_user_meta($userID, 'school', true);
	if($userRole == "seitcm_student" && (current_user_can('seitcm_student_update')||current_user_can('manage_options'))){
		?>
    <h3>Student Info</h3>
    <table class="form-table">
        <tr>
            <th scope="row"><label for="seitcm_englishname"><?php _e('English Name','SEITCM')?></label></th>
            <td><input type="text" class="regular-text" name="seitcm_englishname" id="seitcm_englishname" value="<?php echo get_user_meta($userID, 'englishname', true); ?>" /></td>
        </tr>
    </table>
    <table class="form-table">
        <tr>
            <th scope="row"><label for="seitcm_dob"><?php _e('Date of birth','SEITCM')?></label></th>
            <td><input type="text" class="regular-text" name="seitcm_dob" id="seitcm_dob" value="<?php echo get_user_meta($userID, 'dob', true); ?>" /></td>
        </tr>
    </table>
    <table class="form-table">
        <tr>
            <th scope="row"><label for="seitcm_address"><?php _e('Address','SEITCM')?></label></th>
            <td><input type="text" class="regular-text" name="seitcm_address" id="seitcm_address" value="<?php echo get_user_meta($userID, 'address', true); ?>" /></td>
        </tr>
    </table>
    
    <table class="form-table seitcm_form">
    	<tr>
            <th scope="row"><label for="seitcm_user_school"><?php _e('School','SEITCM')?></label></th>
            <td>
            	<select name="seitcm_user_school" id="seitcm_user_school" class = "seitcm_select_parent">
                    <option value="0" <?php if ($seitcm_user_class == 0) echo ' selected="selected"'; ?>><?php _e('Chose school ...','SEITCM')?></option>
					<?php 
                    global $seitcm_schools;
                    foreach ($seitcm_schools as $seitcm_school)
                    {
                    	if($seitcm_school['status']){
	                    ?>
                    <option value="<?php echo $seitcm_school['key']?>" <?php if ($seitcm_user_school == $seitcm_school['key']) echo ' selected="selected"'; ?>><?php echo $seitcm_school['school']?></option>
                    <?php 
                    	}
                    }
                    ?>
                    </select>
            </td>
        </tr>
	</table>
	<table class="form-table">
		<tr>
            <th scope="row"><label for="seitcm_user_class"><?php _e('Class','SEITCM')?></label></th>
            <td>
            	<div class="seitcm_select_child_wrapper" style="width: 200px; height: 30px; overflow: hidden; float: left; clear: right;">
                    <select title="0" name="seitcm_user_class_select" id = "seitcm_user_class_select" class = "seitcm_select_child" disabled="disabled"></select>
                    <?php 
                    global $seitcm_schools;
                    foreach ($seitcm_schools as $seitcm_school)
                    {
                    	if($seitcm_school['status']){
	                    ?>
                    	<select title="<?php echo $seitcm_school['key']?>" name="seitcm_user_class" id = "seitcm_user_class" class = "seitcm_select_child">
                    		<option value="0" <?php if ($seitcm_user_class == 0) echo ' selected="selected"'; ?>><?php _e('Chose class ...','SEITCM')?></option>
                    		<?php 
                    		foreach ($seitcm_school['class'] as $seitcm_class)
                    		{
	                    	?>
                    		<option value="<?php echo $seitcm_class['key']?>" <?php if ($seitcm_user_class == $seitcm_class['key']) echo ' selected="selected"'; ?>>
                    		<?php echo $seitcm_class['classname']?>
                    		</option>
                    		<?php
	                    	}
                    		?>
                    	</select>
                    <?php
                    	}
                    }
                    ?>
                </div>
            </td>
        </tr>
    </table>
    <table class="form-table">
        <tr>
            <th scope="row"><label for="seitcm_phonenumber"><?php _e('Phone number','SEITCM')?></label></th>
            <td><input type="text" class="regular-text" name="seitcm_phonenumber" id="seitcm_phonenumber" value="<?php echo get_user_meta($userID, 'phonenumber', true); ?>" /></td>
        </tr>
    </table>
<?php
    }
}
function seitcm_extend_update_student($user_id){
	$id = $user_id->ID;
	$englishname = isset($_POST['englishname']) ? $_POST["englishname"] : get_user_meta($id, 'englishname', true);
	$dob = isset($_POST['dob']) ? $_POST["dob"] : get_user_meta($id, 'dob', true);
	$school = isset($_POST['school']) ? $_POST["school"] : get_user_meta($id, 'school', true);
	$class = isset($_POST['class']) ? $_POST["class"] : get_user_meta($id, 'class', true);
	$address = isset($_POST['address']) ? $_POST["address"] : get_user_meta($id, 'address', true);
	$phonenumber = isset($_POST['phonenumber']) ? $_POST["phonenumber"] : get_user_meta($id, 'phonenumber', true);
	
	update_user_meta($id, "englishname", $englishname);
	update_user_meta($id, "dob", $dob);
	update_user_meta($id, "school", $school);
	update_user_meta($id, "class", $class);
	update_user_meta($id, "address", $address);
	update_user_meta($id, "phonenumber", $phonenumber);
}
/*
 	
	//add custom fields to user profile
	//add_action( 'show_user_profile', 'seitcm_add_custom_user_profile_fields' );
	add_action( 'edit_user_profile', 'seitcm_extend_update_student_form' );
	
	//save custom fields when update user profile
	//add_action( 'personal_options_update', 'seitcm_save_custom_user_profile_fields' );
	add_action( 'edit_user_profile_update', 'seitcm_extend_update_student' );
 * */
?>